package com.changgou.goods.controller;

import com.changgou.entity.PageResult;
import com.changgou.entity.Result;
import com.changgou.entity.StatusCode;
import com.changgou.goods.service.BrandService;
import com.changgou.goods.pojo.Brand;
import com.github.pagehelper.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @Author: HuangFu
 * @Date: 2020/1/14 21:19
 * @Description: 品牌控制层
 */
@RestController
@RequestMapping("/brand")
@CrossOrigin // 开启跨域访问
public class BrandController {
    @Autowired
    private BrandService brandService;

    /**
     * 查询所有品牌信息
     * @return
     */
    @GetMapping
    public Result<List<Brand>> findAll(){
        List<Brand> brandList = brandService.findAll();
        return new Result(true, StatusCode.OK,"查询成功",brandList);
    }

    /**
     * 根据ID查询实体数据
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result<Brand> findById(@PathVariable(value = "id") Integer id){
        Brand brand = brandService.findById(id);
        return new Result(true,StatusCode.OK,"查询成功",brand);
    }

    /**
     * 新增品牌
     * @param brand
     */
    @PostMapping
    public Result add(@RequestBody Brand brand){
        brandService.add(brand);
        return new Result(true,StatusCode.OK,"保存成功");
    }

    /**
     * 更新数据
     * @param brand
     * @return
     */
    @PostMapping("/{id}")
    public Result update(@RequestBody Brand brand,@PathVariable("id")Integer id){
        brand.setId(id);
        brandService.update(brand);
        return new Result(true,StatusCode.OK,"更新成功");
    }

    /**
     * 查询品牌根据条件
     * @param searchMap
     * @return
     */
    @GetMapping("/search")
    public Result<List<Brand>> findList(@RequestBody Map<String,Object> searchMap){
        List<Brand> list = brandService.findList(searchMap);
        return new Result(true,StatusCode.OK,"查询成功",list);
    }

    /**
     * 分页查询
     * @param page
     * @param size
     * @return
     */
    @GetMapping(value = "/search/{page}/{size}" )
    public Result<Brand> findPage(@PathVariable("page") int page, @PathVariable("size") int size){
        Page<Brand> brandPage = brandService.findPage(page, size);
        PageResult pageResult = new PageResult(brandPage.getTotal(),brandPage.getResult());
        return new Result(true,StatusCode.OK,"查询成功",pageResult);
    }

    /**
     * 根据条件查询分页数据
     * @param searchMap
     * @param page
     * @param size
     * @return
     */
    @GetMapping(value = "/searchPage/{page}/{size}" )
    public Result<Brand> findPage(@RequestParam Map searchMap,@PathVariable("page") int page, @PathVariable("size") int size){
        Page<Brand> brandPage = brandService.findPage(searchMap,page, size);
        PageResult pageResult = new PageResult(brandPage.getTotal(),brandPage.getResult());
        return new Result(true,StatusCode.OK,"查询成功",pageResult);
    }

    /**
     * 根据分类查询品牌信息
     * @param categoryName
     * @return
     */
    @GetMapping("/category/{categoryName}")
    public Result<List<Map>> findListByCategoryName(@PathVariable("categoryName") String categoryName){
        List<Map> list =brandService.findListByCategoryName(categoryName);
        return new Result(true,StatusCode.OK,"查询成功",list);
    }


}
